package com.xiaoxin.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxin.dubbo.api.BlackListApi;
import com.xiaoxin.dubbo.api.QuestionApi;
import com.xiaoxin.dubbo.api.SettingApi;
import com.xiaoxin.interceptor.UserHolder;
import com.xiaoxin.model.domain.Question;
import com.xiaoxin.model.domain.Settings;
import com.xiaoxin.model.domain.UserInfo;
import com.xiaoxin.model.vo.PageResult;
import com.xiaoxin.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author xiaoxiaode
 * @date 2021-08-30-19:47
 **/
@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingApi settingApi;

    @DubboReference
    private BlackListApi blackListApi;

    // 查询通用设置
    public SettingsVo setting() {
        SettingsVo vo = new SettingsVo();
        // 获取用户id
        Long id = UserHolder.getId();
        // String phone = settingApi.findPhone(id);
        vo.setPhone(UserHolder.getMobile());
        // vo.setPhone(phone);
        // 获取用户的陌生人问题
        Question question = questionApi.findByUserId(id);
        String txt = question == null ? "你是来拉屎的吧" : question.getTxt();
        vo.setStrangerQuestion(txt);
        // 获取用户的app通知开关数据
        Settings settings = settingApi.findByUserId(id);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    // 设置陌生人问题
    public void saveQuestion(String content) {
        // 获取当前用户id
        Long id = UserHolder.getId();
        // 调用api查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(id);
        // 判断问题是否存在
        if (question == null) {
            // 不存在,保存
            question = new Question();
            question.setUserId(id);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            // 存在,更新
            question.setTxt(content);
            questionApi.update(question);
        }

    }

    // 通知设置
    public void saveSettings(Map map) {
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        boolean LikeNotification = (boolean) map.get("likeNotification");
        boolean PinglunNotification = (boolean) map.get("pinglunNotification");
        // 获取当前线程id
        Long id = UserHolder.getId();
        // 根据用户id,查询用户的通知设置
        Settings settings = settingApi.findByUserId(id);
        // 判断
        if (settings == null) {
            // 保存
            settings = new Settings();
            settings.setUserId(id);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(LikeNotification);
            settings.setPinglunNotification(PinglunNotification);
            settingApi.save(settings);
        } else {
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(LikeNotification);
            settings.setPinglunNotification(PinglunNotification);
            settingApi.update(settings);
        }

    }

    // 分页查询黑名单
    public PageResult blacklist(int page, int size) {
        // 获取当前对象的id
        Long id = UserHolder.getId();
        // 调用api查询用户的黑名单列表 IPage对象
        IPage<UserInfo> iPage = blackListApi.findByUserId(id, page, size);
        // 对象转化,将查询的IPage对象的内容封装到PageResult中
        PageResult pageResult = new PageResult(page, size, iPage.getTotal(), iPage.getRecords());
        // 返回
        return pageResult;
    }

    // 取消黑名单
    public void deleteBlackList(Long uid) {
        // 获取当前用户id
        Long id = UserHolder.getId();
        // 调用service层方法
        blackListApi.deleteBlackList(id, uid);

    }
}
